package com.lyf.baoxian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lyf.baoxian.R;
import com.lyf.baoxian.activity.BuyTaoCanActivity;
import com.lyf.baoxian.activity.ShowTaoCanActivity;
import com.lyf.baoxian.adapter.TaoCanAdapter;
import com.lyf.baoxian.adapter.TaoCanAdapter2;
import com.lyf.baoxian.bean.TestTC;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘亚飞 on 2016/11/30.
 */

public class TaoCanFragment extends BaseFragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    /*@Bind(R.id.lv_taocan)
     ListView lv_taocan;*/
    /*@Bind(R.id.item1)
    ViewGroup viewGroup1;
    @Bind(R.id.item2)
    ViewGroup viewGroup2;
    @Bind(R.id.item3)
    ViewGroup viewGroup3;*/
    @Bind(R.id.rg)
    RadioGroup rg;
    @Bind(R.id.rb_zc)
    RadioButton rb_zc;
    @Bind(R.id.rb_fzc)
    RadioButton rb_fzc;

    @Bind(R.id.lv)
    ListView lv;

    //政策保险
    private TaoCanAdapter adapter;
    //非政策保险
    private TaoCanAdapter2 adapter2;

    List<TestTC> data1;
    List<TestTC> data2;


    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        initData();
       /* viewGroup1.setOnClickListener(this);
        viewGroup2.setOnClickListener(this);
        viewGroup3.setOnClickListener(this);*/

    }

    /**
     * 初始化数据
     */
    private void initData() {
        data1 = new ArrayList<>();
        data2 = new ArrayList<>();

        data1.add(new TestTC(R.drawable.shuidao,"水稻种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        data1.add(new TestTC(R.drawable.yumi,"玉米种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        data1.add(new TestTC(R.drawable.xiaomai,"小麦种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        data1.add(new TestTC(R.drawable.tudou,"马铃薯种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        for (int i = 0; i < 10; i++) {
//            data1.add(new TestTC(R.drawable.shuidao,"水稻种植成本保险"+i,"属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
            data2.add(new TestTC(R.drawable.lv1,"荷兰猪养殖成本保险"+i,"属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        }


        adapter = new TaoCanAdapter(mActivity);
        adapter.setDatas(data1);
        lv.setAdapter(adapter);
        rg.check(R.id.rb_zc);
        rg.setOnCheckedChangeListener(this);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mActivity,"位置w"+i,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mActivity, BuyTaoCanActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected View addLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_taocan, null);
        return view;
    }

    @Override
    public void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*case R.id.item1:
                Intent intent = new Intent(mActivity, ShowTaoCanActivity.class);
                startActivity(intent);
                break;

            case R.id.item2:
                Intent intent2 = new Intent(mActivity, ShowTaoCanActivity.class);
                startActivity(intent2);
                break;

            case R.id.item3:
                Intent intent3 = new Intent(mActivity, ShowTaoCanActivity.class);
                startActivity(intent3);
                break;*/
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rb_zc:  //政策保险
                rg.setBackgroundResource(R.drawable.left);
                if (adapter == null) {
                    adapter = new TaoCanAdapter(mActivity);
                }
                adapter.setDatas(data1);
                lv.setAdapter(adapter);

                break;

            case R.id.rb_fzc: //非政策保险
                rg.setBackgroundResource(R.drawable.right);
                if (adapter2 == null) {
                    adapter2 = new TaoCanAdapter2(mActivity);
                }
                adapter2.setDatas(data2);
                lv.setAdapter(adapter2);

                break;
        }

    }

}
